package com.heyun.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heyun.bean.HeyunBasicInfo;
import com.heyun.bean.Message;
import com.heyun.service.BasicInfoService;



@Controller
public class BasicInfoController {

	@Autowired
	private BasicInfoService basicInfoService;
	
	/**
	 * 分页查看基础信息
	 * @param pn 传入参数为页码
	 * @return 返回所要查询的页码的信息
	 */
	@RequestMapping("/basicInfo")
	@ResponseBody
	public Message listBasicInfo(@RequestParam(value="pn",defaultValue="1")Integer pn){
		//引入pageHelper插件
		//在查询之前调用PageHelper.startPage(pn, 5)方法，传入要查询的页数和每页显示的数据数量
		PageHelper.startPage(pn, 5);
		//startPage()后面紧跟的就是一个分页查询的数据
		List<HeyunBasicInfo> listBasicInfo = basicInfoService.listBasicInfo();
		//使用PageInfo包装查询后的结果，只要将PageInfo交个页面就行了
		//封装了详细的分页信息，包括查询出来的数据，传入连续显示的页数
		PageInfo<HeyunBasicInfo> page = new PageInfo<HeyunBasicInfo>(listBasicInfo,5);
		
		return Message.success().add("pageInfo", page);
	}
	
	/**
	 * 通过keyword模糊查询基础信息
	 * @param keyword 传入参数为String型keyword
	 * @return 返回查询到的HeyunBasicInfo对象
	 */
	@RequestMapping(value="/basicInfo_byLike",method=RequestMethod.GET)
	@ResponseBody
	public Message getBasicInfo(@RequestParam(value="pn",defaultValue="1")Integer pn,String keyword){
		System.out.println("模糊查询方法条件"+keyword);
		//引入pageHelper插件
		//在查询之前调用PageHelper.startPage(pn, 5)方法，传入要查询的页数和每页显示的数据数量
		PageHelper.startPage(pn, 5);
		//startPage()后面紧跟的就是一个分页查询的数据
		List<HeyunBasicInfo> basicInfo = basicInfoService.getBasicInfoByKeyword(keyword);
		//使用PageInfo包装查询后的结果，只要将PageInfo交个页面就行了
		//封装了详细的分页信息，包括查询出来的数据，传入连续显示的页数
		PageInfo<HeyunBasicInfo> page = new PageInfo<HeyunBasicInfo>(basicInfo,5);
		
		return Message.success().add("pageInfo", page);
	}
	
	/**
	 * 新增基础信息方法
	 * @param basicInfo 传入参数为HeyunBasicInfo对象
	 * @return 返回执行结果：成功or失败
	 */
	@RequestMapping(value="/basicInfo",method=RequestMethod.POST)
	@ResponseBody
	public Message insertBaisc(HeyunBasicInfo basicInfo,BindingResult result){
		System.out.println("添加的基础信息："+basicInfo);
		basicInfoService.insertBasicInfo(basicInfo);
		return Message.success();
	}
	
	/**
	 * 通过ID查询单个基础信息
	 * @param basicId 传入参数为int型basicId
	 * @return 返回查询到的HeyunBasicInfo对象
	 */
	@RequestMapping(value="/basicInfo/{basicId}",method=RequestMethod.GET)
	@ResponseBody
	public Message getBasicInfoById(@PathVariable("basicId")Integer basicId){
		HeyunBasicInfo basicInfo = basicInfoService.getBasicInfoById(basicId);
		return Message.success().add("basicInfo", basicInfo);
	}
	
	/**
	 * 更新基本信息
	 * @param basicInfo 传入参数为基本信息的对象
	 * @return 返回更新结果：成功or失败
	 */
	@RequestMapping(value="/basicInfo/{basicId}",method=RequestMethod.PUT)
	@ResponseBody
	public Message updateBasic(HeyunBasicInfo basicInfo){
		System.out.println("更新的数据："+basicInfo);
		basicInfoService.updateBasicInfo(basicInfo);
		return Message.success();
	}
	
	/**
	 * 删除基本信息的方法
	 * @param basicId 传入参数为int型basicId
	 * @return 返回删除的结果：成功or失败
	 */
	@RequestMapping(value="/basicInfo/{basicId}",method=RequestMethod.DELETE)
	@ResponseBody
	public Message deleteBasic(@PathVariable("basicId") Integer basicId){
		basicInfoService.deleteBasicInfo(basicId);
		return Message.success();
	}
}
